package com.ourcraft.informationcollectionsystem.controller.province;


import com.alibaba.fastjson.JSONObject;
import com.ourcraft.informationcollectionsystem.entity.DataReportForm;
import com.ourcraft.informationcollectionsystem.entity.DTO.DataReportFormDTO;
import com.ourcraft.informationcollectionsystem.service.province.formQuery.FormQueryService;
import com.ourcraft.informationcollectionsystem.utils.authority.AuthorityCode;
import com.ourcraft.informationcollectionsystem.utils.authority.AuthorityValidator;
import com.ourcraft.informationcollectionsystem.utils.response.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RequestMapping("province/formQuery/")
@RestController
@CrossOrigin
@AuthorityValidator(code = AuthorityCode.DATA_FORM_QUERY)
public class FormQueryController {
    @Autowired
    private FormQueryService formQueryService;

    @PostMapping("getRequiredFormList")
    public Result<List<DataReportForm>> getRequiredFormList(@RequestBody JSONObject param, HttpServletRequest request){
        var formQuery = formQueryService.getRequiredFormList(
                param.getJSONObject("company"),
                param.getJSONObject("investigationPeriod")
        );
        return Result.success(formQuery);
    }
    @PostMapping("getRequiredFormListJoin")
    public Result<List<DataReportFormDTO>> getRequiredFormListJoin(@RequestBody JSONObject param, HttpServletRequest request){
        var formQuery = formQueryService.getRequiredFormListJoin(
                param.getJSONObject("company"),
                param.getJSONObject("investigationPeriod")
        );
        return Result.success(formQuery);
    }

    @GetMapping("exportCurrentFormList")
    public ResponseEntity<Resource> exportCurrentFormList(@RequestParam String params, HttpServletRequest request) throws UnsupportedEncodingException {
        var json = JSONObject.parseObject(URLDecoder.decode(params, StandardCharsets.UTF_8));
        return formQueryService.exportCurrentFormList(
                json.getJSONObject("company"),
                json.getJSONObject("investigationPeriod")
        );
    }
}
